package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.PopWindowWpAdapter;
import com.xianbeiying.app.ckgl.adapter.VarbGridViewAdapter;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.FaMenBean;
import com.xianbeiying.app.ckgl.bean.FaMenTypeBean;
import com.xianbeiying.app.ckgl.bean.JsonBean;
import com.xianbeiying.app.ckgl.bean.JsonDataBean;
import com.xianbeiying.app.ckgl.bean.OpenFmBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.data.DataUtil;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.StringUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.MyGridView;
import com.xianbeiying.app.ckgl.widght.popwindow.LayoutPopWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能操作
 */
public class FunctionOperateActivity extends BaseActivity implements View.OnClickListener, HttpCallBackUtil {
    private RadioGroup rd_fo_manul;
    private RadioButton fo_open;
    private RadioButton fo_close;
    private ImageView iv_title_left;
    private TextView tv_title;
    //水平滑动的
    private LinearLayout ly_fohorizontal;
    private LinearLayout ly_choice_sb;
    private View va_line;
    private TextView tv_va_all;

    private ImageView iv_fo_fj, iv_fo_jl;
    private TextView tv_fo_content;
    private ImageView iv_fo_choice;
    //泡泡窗口
    private LayoutPopWindow choicePop;
    private String[] datas;
    private String type;

    private TextView tv_fo_cgg, tv_fo_cfl, tv_fo_cszd;
    private SetModel setModel;
    private GetModel getModel;
    private LinearLayout ly_choice_fo;
    private MyGridView fogrid;
    private ArrayList<String> vaManulList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_function_operate);
        initView();
    }

    private void initView() {
        va_line = (View) findViewById(R.id.va_line);
        tv_fo_cgg = (TextView) findViewById(R.id.tv_fo_cgg);
        tv_fo_cfl = (TextView) findViewById(R.id.tv_fo_cfl);
        tv_fo_cszd = (TextView) findViewById(R.id.tv_fo_cszd);
        tv_va_all = (TextView) findViewById(R.id.tv_va_all);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        ly_fohorizontal = (LinearLayout) findViewById(R.id.ly_fohorizontal);
        ly_choice_fo = (LinearLayout) findViewById(R.id.ly_choice_fo);
        ly_choice_sb = (LinearLayout) findViewById(R.id.ly_choice_sb);
        rd_fo_manul = (RadioGroup) findViewById(R.id.rd_fo_manul);
        fo_open = (RadioButton) findViewById(R.id.fo_open);
        fo_close = (RadioButton) findViewById(R.id.fo_close);
        iv_fo_fj = (ImageView) findViewById(R.id.iv_fo_fj);
        iv_fo_jl = (ImageView) findViewById(R.id.iv_fo_jl);
        tv_fo_content = (TextView) findViewById(R.id.tv_fo_content);
        iv_fo_choice = (ImageView) findViewById(R.id.iv_fo_choice);
        fogrid = (MyGridView) findViewById(R.id.fogrid);
        fo_open.setChecked(true);
        tv_title.setText("功能操作");
        iv_title_left.setOnClickListener(this);
        tv_va_all.setOnClickListener(this);
        tv_fo_cgg.setText("当前");
        tv_fo_cgg.setSelected(true);
        datas = DataUtil.famen;
        type = "号阀门";
        getModel = new GetModel(this);
        setModel = new SetModel(this);
        setModel.addResponseListener(this);
        getModel.addResponseListener(this);
        tv_fo_cgg.setOnClickListener(this);
        tv_fo_cfl.setOnClickListener(this);
        tv_fo_cszd.setOnClickListener(this);
        iv_fo_choice.setOnClickListener(this);
        iv_fo_fj.setOnClickListener(this);
        iv_fo_jl.setOnClickListener(this);
        getArrayBoolen = StringUtil.getList(datas.length);
        initData();
        manulenevt();
    }


    private void initData() {
        if (MyApplication.devicesBean != null){
            LogUtils.e("---------进入路");
            devicesBean = MyApplication.devicesBean;
            getData(devicesBean.getDEVICES().size(), 0);
            getModel.QUWEYDEVSDetauls("GET_ALLDEVICES", devicesBean.getDEVICES().get(0).getSystemid(), true);
        } else {
            getModel.QUWEYDEVS(true);
        }
    }

    private List<View> t_list;

    private void getData(int size, final int id) {
        t_list = new ArrayList<View>();
        for (int i = 0; i < size; i++) {
            //每个textview的宽度
            View view = LayoutInflater.from(this).inflate(R.layout.fragment_va_item, null);
            LinearLayout ly_va_cicy = (LinearLayout) view.findViewById(R.id.ly_va_cicy);
            final TextView tv_va_cicy = (TextView) view.findViewById(R.id.tv_va_cicy);
            tv_va_cicy.setText((i + 1) + "\n号棚");
            //添加集合
            t_list.add(view);
            //绑定
            ly_va_cicy.setTag(i);
            ly_fohorizontal.addView(view);
            ly_va_cicy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int m = (Integer) v.getTag();
                    int length = t_list.size();
                    for (int i = 0; i < length; i++) {
                        if (i == m) {
                            faId = m;
                            t_list.get(m).findViewById(R.id.ly_va_cicy).setSelected(true);
                            t_list.get(m).findViewById(R.id.tv_va_cicy).setSelected(true);
                        } else {
                            t_list.get(i).findViewById(R.id.ly_va_cicy).setSelected(false);
                            t_list.get(i).findViewById(R.id.tv_va_cicy).setSelected(false);
                        }
                    }
                }
            });
        }
    }

    private PopWindowWpAdapter popWindowWpAdapter;
    private ArrayList<Boolean> getArrayBoolen;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            //全部跳转
            case R.id.tv_va_all:
                va_line.setVisibility(View.GONE);
                tv_va_all.setVisibility(View.GONE);
                break;
            //风机
            case R.id.iv_fo_fj:
                showDefautlfj();
                iv_fo_fj.setSelected(true);
                datas = DataUtil.fenji;
                type = "号风机";
                popWindowWpAdapter = null;
                ly_choice_fo.setVisibility(View.GONE);
                tv_fo_content.setText("1号风机");
                getArrayBoolen = StringUtil.getList(datas.length);
                break;
            //卷帘
            case R.id.iv_fo_jl:
                showDefautlfj();
                iv_fo_jl.setSelected(true);
                datas = DataUtil.juanlian;
                type = "号卷帘";
                tv_fo_content.setText("1号卷帘");
                popWindowWpAdapter = null;
                ly_choice_fo.setVisibility(View.GONE);
                getArrayBoolen = StringUtil.getList(datas.length);
                break;
            //选择数据
            case R.id.iv_fo_choice:
                //默认灌溉
                choicePop = new LayoutPopWindow(this, R.layout.pop_window_wp, tv_fo_content);
                ListView listView = choicePop.getView(R.id.pop_wp_lists);
                if (type.equals("号加热器")) {
                    com.xianbeiying.app.ckgl.util.DataUtil.setHeight(listView, 320);
                    fogrid.setNumColumns(3);
                } else {
                    com.xianbeiying.app.ckgl.util.DataUtil.setHeight(listView, 280);
                }
                popWindowWpAdapter = new PopWindowWpAdapter(this, datas, type);
                popWindowWpAdapter.setIsBoolean(getArrayBoolen);
                choicePop.setViewOnClickListener(R.id.tv_qdpop_wp, this);
                listView.setAdapter(popWindowWpAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (popWindowWpAdapter.getIsBoolean().get(position)) {
                            popWindowWpAdapter.getIsBoolean().set(position, false);
                        } else {
                            popWindowWpAdapter.getIsBoolean().set(position, true);
                        }
                        getArrayBoolen = popWindowWpAdapter.getIsBoolean();
                        popWindowWpAdapter.notifyDataSetChanged();
                    }
                });
                break;
            //确定
            case R.id.tv_qdpop_wp:
                if (choicePop != null && choicePop.isShowing()) {
                    int length = popWindowWpAdapter.getIsBoolean().size();
                    vaManulList.clear();
                    for (int i = 0; i < length; i++) {
                        //选中的
                        if (popWindowWpAdapter.getIsBoolean().get(i)) {
                            vaManulList.add(datas[i] + "");
                        }
                    }
                    ly_choice_fo.setVisibility(View.VISIBLE);
                    fogrid.setAdapter(new VarbGridViewAdapter(FunctionOperateActivity.this, DataUtil.getRListData(vaManulList), type));
                    choicePop.dismiss();
                }
                break;
            //选择灌溉
            case R.id.tv_fo_cgg:
                showDefautlChoice();
                tv_fo_cgg.setSelected(true);
                tv_fo_cgg.setText("当前");
                datas = DataUtil.famen;
                type = "号阀门";
                tv_fo_content.setText("1号阀门");
                getArrayBoolen = StringUtil.getList(datas.length);
                break;
            //选择凤帘
            case R.id.tv_fo_cfl:
                showDefautlChoice();
                showDefautlfj();
                iv_fo_fj.setSelected(true);
                tv_fo_cfl.setSelected(true);
                tv_fo_cfl.setText("当前");
                iv_fo_fj.setSelected(true);
                ly_choice_sb.setVisibility(View.VISIBLE);
                datas = DataUtil.fenji;
                type = "号风机";
                tv_fo_content.setText("1号风机");
                getArrayBoolen = StringUtil.getList(datas.length);
                break;
            //选择加热器
            case R.id.tv_fo_cszd:
                showDefautlChoice();
                tv_fo_cszd.setSelected(true);
                tv_fo_cszd.setText("当前");
                datas = DataUtil.szlight;
                type = "号加热器";
                tv_fo_content.setText("1号加热器");
                getArrayBoolen = StringUtil.getList(datas.length);
                break;
        }
    }

    //设置操作功能的默认显示状态
    private void showDefautlChoice() {
        tv_fo_cfl.setText("选择");
        tv_fo_cgg.setText("选择");
        tv_fo_cszd.setText("选择");
        ly_choice_sb.setVisibility(View.GONE);
        tv_fo_cgg.setSelected(false);
        tv_fo_cfl.setSelected(false);
        tv_fo_cszd.setSelected(false);
        popWindowWpAdapter = null;
        ly_choice_fo.setVisibility(View.GONE);

    }

    //设置风机卷帘的默认显示状态
    private void showDefautlfj() {
        iv_fo_fj.setSelected(false);
        iv_fo_jl.setSelected(false);
    }

    private void manulenevt(){
        fo_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                //打开
            }
        });
        fo_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                //关闭
            }
        });
    }

    private JsonDataBean jsonDataBean;
    //网络返回值
    private int faId;
    private DevicesBean devicesBean;
    private FaMenBean faMenBean;
    private ArrayList<FaMenBean.VBean.STATUSBean> listFa;
    private FaMenTypeBean faMenTypeBean;
    private OpenFmBean openFmBean;
    private String[] fmType;

    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals(UrlBean.QUWEYDEVS)) {
            if (content != null) {
                devicesBean = JSON.parseObject(new String((byte[])content), DevicesBean.class);
                MyApplication.devicesBean = devicesBean;
                getData(devicesBean.getDEVICES().size(), 0);
                getModel.QUWEYDEVSDetauls("GET_ALLDEVICES", devicesBean.getDEVICES().get(0).getSystemid(), false);
            }
        }
        if (url.equals("大棚信息")) {
            if (content != null) {
                faMenBean = JSON.parseObject(new String((byte[])content), FaMenBean.class);
                jsonDataBean= JsonBean.setSFmData(devicesBean,listFa,faId);
                getModel.GET_SWITCH_STATUS(jsonDataBean, false);
            }
        }
        if (url.equals("阀门信息")) {
            if (content != null) {
                faMenTypeBean = JSON.parseObject(new String((byte[])content), FaMenTypeBean.class);
                if (faMenBean.isR()) {
                    fmType =DataUtil.getFmOc(faMenTypeBean);
                } else {
                    ToastUtil.showInfo(FunctionOperateActivity.this, "操作失败");
                }
            }
        }
        if (url.equals("设置阀门")) {
            if (content != null) {
                openFmBean = JSON.parseObject(new String((byte[])content), OpenFmBean.class);
                if (openFmBean.isR()) {
                    ToastUtil.showInfo(FunctionOperateActivity.this, "操作成功");
                    jsonDataBean= JsonBean.setSFmData(devicesBean,listFa,faId);
                    getModel.GET_SWITCH_STATUS(jsonDataBean, false);

                } else {
                    ToastUtil.showInfo(FunctionOperateActivity.this, "操作失败");
                }

            }
        }
    }
}
